﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.XtraEditors;
using System.Linq;
using System.Threading.Tasks;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys.Filter;
using XCI.Sys.Helper;
using XCI.Sys.Model;
using XCI.Sys.Service;

namespace XCI.Windows
{
    /// <summary>
    /// 编辑模块页面,选择上级下拉TreeList
    /// </summary>
    [System.ComponentModel.DesignerCategory("Code")]
    public class EditParentModuleTreeListLookUpEdit : TreeListLookUpEdit
    {
        private ModuleHelper _moduleHelper;
        private DevExpress.Utils.ImageCollection treeImageList;

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;

            var treelist = this.Properties.TreeList;
            if (treelist == null) return;

            if (_moduleHelper == null) _moduleHelper = new ModuleHelper(treelist);
            this.treeImageList = new DevExpress.Utils.ImageCollection();
            WinFormHelper.SetImageListSizeByDpiRatio(treeImageList);
            this.Properties.ValueMember = "Id";
            this.Properties.DisplayMember = "Name";
            this.Properties.PopupWidthMode = PopupWidthMode.UseEditorWidth;
            this.ButtonClick += EditParentModuleTreeListLookUpEdit_ButtonClick;
            this.EditValueChanged += EditParentModuleTreeListLookUpEdit_EditValueChanged;

            treelist.Columns.AddRange(new[] {
                new DevExpress.XtraTreeList.Columns.TreeListColumn
                {
                    Caption = "名称",
                    FieldName = "NameCode",
                    Name = "treeColName",
                    Visible = true,
                    VisibleIndex = 0
                }});
            treelist.KeyFieldName = "Id";
            treelist.ParentFieldName = "ParentId";
            treelist.OptionsBehavior.AutoPopulateColumns = true;
            treelist.OptionsSelection.EnableAppearanceFocusedCell = false;
            treelist.OptionsView.FocusRectStyle = DevExpress.XtraTreeList.DrawFocusRectStyle.None;
            treelist.OptionsView.ShowColumns = false;
            treelist.OptionsView.ShowHorzLines = false;
            treelist.OptionsView.ShowIndicator = false;
            treelist.OptionsView.ShowVertLines = false;
            treelist.OptionsView.ShowColumns = false;
            treelist.OptionsView.ShowAutoFilterRow = true;
            treelist.OptionsView.ShowFilterPanelMode = DevExpress.XtraTreeList.ShowFilterPanelMode.Never;
            treelist.OptionsMenu.EnableNodeMenu = false;
            treelist.StateImageList = this.treeImageList;
            treelist.GetStateImage += Treelist_GetStateImage;
        }

        private void EditParentModuleTreeListLookUpEdit_EditValueChanged(object sender, System.EventArgs e)
        {
            if (this.Properties.Buttons.Count >= 1)
            {
                this.Properties.Buttons[1].Visible = this.EditValue != null;
            }
        }

        private void EditParentModuleTreeListLookUpEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (this.Properties.Buttons.Count >= 1 && e.Button.Index == 1)
            {
                this.EditValue = null;
                WinFormHelper.SetTreeRootValue(this, null);
            }
        }

        private void Treelist_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
        {
            e.NodeImageIndex = _moduleHelper.GetModuleImageIndex(e.Node);
        }

        public async Task LoadData(bool created, SysModule module)
        {
            var list = await SysModuleService.Instance.SelectListAsync(new SysModuleFilter());
            if (!created)//修改时移除当前编辑的记录以及所有下级
            {
                var childs = TreeOperateHelper.GetChildren(list, module, true);
                list.Delete(p => childs.Any(x => x.Id.Equals(p.Id)));
            }

            this.Properties.TreeList.DataSource = list;
            await _moduleHelper.InitAsync(treeImageList);
            this.Properties.TreeList.ExpandToLevel(0);
        }
    }
}